package cn.momoshenchi.controller;

import cn.momoshenchi.pojo.*;
import cn.momoshenchi.service.*;
import cn.momoshenchi.util.ResultInfo;
import cn.momoshenchi.util.AssertUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @version : 1.0
 * @author: momoshenchi
 * @date: 2021/6/7 - 15:59
 */
@RestController
@RequestMapping("/user")
public class UserController
{
    @Autowired
    private UserService userService;

    @Autowired
    private UserLoginService userLoginService;

    @Autowired
    private UserCollectArticleService userCollectArticleService;

    @Autowired
    private UserThumbService userThumbService;

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/{id}")
    @Operation(summary = "根据Id查询用户信息")
    public ResultInfo info(@PathVariable Long id)
    {
        User user = userService.getById(id);
        return  new ResultInfo(user);
    }


    @PostMapping("/update")
    @Operation(summary = "修改用户信息")
    public  ResultInfo update(@RequestBody @Valid User user)
    {
        AssertUtil.isTrue(!userService.updateById(user),"修改失败");
        return  new ResultInfo();
    }


    @PostMapping("/collect/article")
    @Operation(summary = "用户收藏文章")
    public  ResultInfo insertArt(@RequestBody UserCollectArticle userCollectArticle)
    {
        AssertUtil.isTrue(!userCollectArticleService.save(userCollectArticle),"收藏失败");
        return  new ResultInfo();
    }

    @PostMapping("/thumb/article")
    @Operation(summary = "用户点赞文章")
    public  ResultInfo insertThu(@RequestBody UserThumb userThumb)
    {
        AssertUtil.isTrue(!userThumbService.save(userThumb),"点赞失败");
        return  new ResultInfo();
    }

    @GetMapping("/collect/{userId}")
    @Operation(summary = "查询用户收藏的文章")
    public  ResultInfo selectArt(@PathVariable Long userId)
    {
        List<UserCollectArticle> userCollectArticle = userCollectArticleService.selectByUserId(userId);
        return  new ResultInfo(userCollectArticle);
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public  ResultInfo login(@RequestBody @Valid SysUser user)
    {

        SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq("username",user.getUsername()));
        AssertUtil.isTrue(sysUser==null,"手机号码不存在");
        AssertUtil.isTrue(!sysUser.getPassword().equals(user.getPassword()),"密码错误");
        UserLogin userLogin=new UserLogin();
        userLogin.setUserId(sysUser.getUserId());
        userLoginService.save(userLogin);
        return  new ResultInfo(sysUser.getUserId());
    }

    @PostMapping("/logout")
    @Operation(summary = "用户退出")
    public  ResultInfo logout(@RequestBody @Valid UserLogin user)
    {
        userLoginService.updateById(user);
        return  new ResultInfo();
    }

    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public  ResultInfo insert(@RequestBody @Valid  SysUser sysUser)
    {

        SysUser sysUserServiceOne = sysUserService.getOne(new QueryWrapper<SysUser>().eq("username", sysUser.getUsername()));
        AssertUtil.isTrue(sysUserServiceOne!=null,"手机号码已存在");
        AssertUtil.isTrue(!sysUserService.save(sysUser),"注册失败");

        User user=new User();
        user.setPhonenum(sysUser.getUsername());
        AssertUtil.isTrue(!userService.save(user),"注册失败");
        return  new ResultInfo();
    }
}
